package edu.tomee.servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import edu.tomee.db.DatabaseConnection;
import edu.tomee.db.UserInfo;
import edu.tomee.mail.MailUtil;

/**
 * Servlet implementation class ForgetPassword
 */
@WebServlet("/forget_password")
public class ForgetPasswordServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       DatabaseConnection db = new DatabaseConnection();
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ForgetPasswordServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		String eid = request.getParameter("T1");
		response.setContentType("text/html");
		UserInfo result = db.forgetPwd(eid);
		out.println("result of forget pswrd "+result);
		if(null==result){
			out.println("Provided email id is not a registered email id");
		}
		else{
			
			StringBuilder ms = new StringBuilder();
			ms.append("Mr/Ms. <b>").append(result.getFname()).append("<br/>");
			ms.append("Your user id is: <b>").append(result.getEid()).append("<br/>");
			ms.append("Your password is : <b>").append(result.getPwd()).append("<br/>");
			ms.append("Note: This is computer generated Mail, Don't reply to this Mail <br/>");
			MailUtil.sendMessage(eid, ms.toString(), "Password Recovery");
			out.println("<html><head><title> Password Recovery</title>");
			request.getRequestDispatcher("/WEB-INF/jspf/header.jsp").include(request, response);
			out.println("</head><body class='metro'>");
			out.println("Your Password is successfully forwarded to your registered email id");
			out.println("</body></html>");
		}
	}

}
